package org.ims.inv.web.action.system;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.system.ModelAppDictionary;
import org.ims.inv.web.action.BaseAppAction;
import org.springframework.beans.BeanUtils;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.log.LogAnnotation;
import cn.trymore.core.util.UtilString;

/**
 * Action: 系统数据字典
 * 
 * @author Jeccy.Zhao
 *
 */
public class SystemDictionaryAction
extends BaseAppAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(SystemDictionaryAction.class);
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 数据字典页面
	 */
	public ActionForward pageDictIndex (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		return mapping.findForward("page.sys.dict.index");
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 数据字典弹框页面
	 */
	public ActionForward dialogDictPage (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String dictId = request.getParameter("id");
			if (UtilString.isNotEmpty(dictId))
			{
				if (this.isObjectIdValid(dictId))
				{
					request.setAttribute("entity", this.serviceAppDictionary.get(dictId));
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据ID不合法!"));
				}
			}
			else
			{
				String dictType = request.getParameter("type");
				if (UtilString.isNotEmpty(dictType))
				{
					request.setAttribute("type", dictType);
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据字典类型不存在!"));
				}
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading dict dialog page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
		
		return mapping.findForward("dialog.sys.dict");
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 加载数据列表
	 */
	public ActionForward actionLoadDictDataGrid (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String dictType = request.getParameter("type");
			if (UtilString.isNotEmpty(dictType))
			{
				request.setAttribute("datagrid", 
					this.serviceAppDictionary.getByType(dictType));
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("数据字典类型不存在!"));
			}
		}
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when loading dict datagrid", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
		
		return mapping.findForward("datagrid.sys.dict");
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 保存数据字典项
	 */
	@LogAnnotation(description="字典项新增或更新, `${dictKey}` : `${dictValue}`",type="数据字典")
	public ActionForward actionSavaDict (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			ModelAppDictionary formEntity = (ModelAppDictionary)form;
			ModelAppDictionary entity = formEntity;
			
			boolean isActionEdit = this.isObjectIdValid(formEntity.getId());
			
			if (isActionEdit)
			{
				// 编辑
				entity = this.serviceAppDictionary.get(formEntity.getId());
				if (entity != null)
				{
					BeanUtils.copyProperties(formEntity, entity, new String[] { "createDate" } );
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据字典实体不存在!"));
				}
			}
			else
			{
				entity.setCreateDate(new Date());
			}
			
			entity.setModifiedDate(new Date());
			
			// 保存
			this.serviceAppDictionary.save(entity);
			
			return ajaxPrint(response, 
					getSuccessCallback("数据字典项保存成功.", CALLBACK_TYPE_CLOSE, CURRENT_NAVTABID, null, false));
		}
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when saving dict data", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
		
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 移除数据字典项
	 */
	@LogAnnotation(description="字典项移除： `${id}` (id)",type="数据字典")
	public ActionForward actionRemoveDict (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String dictId = request.getParameter("id");
			if (this.isObjectIdValid(dictId))
			{
				ModelAppDictionary entity = this.serviceAppDictionary.get(dictId);
				if (entity != null)
				{
					this.serviceAppDictionary.remove(entity);
					
					return ajaxPrint(response, 
							getSuccessCallbackAndReloadCurrent("数据字典项删除成功."));
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据字典实体不存在!"));
				}
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("数据字典ID不合法!"));
			}
			
		}
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when removing dict data", e);
			return ajaxPrint(response, getErrorCallback("数据删除失败,原因:" + e.getMessage()));
		}
		
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 唯一性检测数据字典项值
	 */
	public ActionForward actionUniqueCheckValueByDictType (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		String dictType = request.getParameter("dictType");
		String dictValue = request.getParameter("dictValue");
		String dictId = request.getParameter("dictId");
		
		if (UtilString.isNotEmpty(dictType, dictValue))
		{
			try
			{
				if (!this.serviceAppDictionary.isDictItemExisted(dictType, dictValue, dictId))
				{
					return ajaxPrint(response, AJAX_RESPONSE_TRUE);
				}
			} 
			catch (ServiceException e)
			{
				LOGGER.error("Exception raised when checking dict data", e);
			}
		}
		
		return ajaxPrint(response, AJAX_RESPONSE_FALSE);
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}
}
